<?php

namespace app\service;

use Illuminate\Database\Capsule\Manager as DB;

class combo
{
    public function previewOrder($goods_id, $sku_price)
    {
        $need_address = 0;
        $combo_goods = DB::table('goods')->find($goods_id);
        if (!$combo_goods || !$combo_goods->is_package || $combo_goods->status != 'up') {
            throw new \Exception('套餐不存在');
        }

        $combo_goods_sku = \goodsskupriceModel::where('goods_id', $goods_id)
            ->where('status', 'up')
            ->first();

        $goods_package = DB::table('goods_package')->where('goods_id', $goods_id)->first();
        $goods_info = json_decode($goods_package->relation_goods_info, true);
        $card_info = json_decode($goods_package->card_info, true);

        $total_num = 0;
        $goods_amount = 0;
        $goods_original_amount = 0;
        $goods_list = [];
        foreach ($goods_info as $goods_val) {
            $total_num += $goods_val['number'];
            array_push($goods_list, ['goods_id' => (int) $goods_val['id'], 'goods_num' => (int) $goods_val['number']]);
        }
        $goods = DB::table('goods')->whereIn('id', array_column($goods_list, 'goods_id'))->get();
        foreach ($goods_list as $k => $v) {
            foreach ($goods as $goods_item) {
                if ($goods_item->id == $v['goods_id']) {
                    if ($goods_item->is_sku) {
                        $goods_list[$k] = null;
                    } else {
                        $goods_amount = bcadd($goods_amount, $goods_item->price, 2);
                        $goods_original_amount = bcadd($goods_original_amount, $goods_item->original_price, 2);
                        $goods_list[$k]['goods_title'] = $goods_item->title;
                        $goods_list[$k]['goods_image'] = $goods_item->image;
                        $goods_list[$k]['goods_price'] = floatval($goods_item->price);
                        $nosku_goods_sku_price = DB::table('goods_sku_price')
                            ->where('goods_id', $v['goods_id'])
                            ->where('status', 'up')
                            ->first();
                        $goods_list[$k]['goods_sku_price_id'] = intval($nosku_goods_sku_price->id ?? 0);
                        $goods_list[$k]['goods_sku_price_text'] = $nosku_goods_sku_price->goods_sku_text ?? '';
                    }
                    break;
                }
            }
        }
        $goods_list = array_filter($goods_list);
        foreach ($sku_price as $sku_price_key => $sku_price_val) {
            $parse_key = explode('-', $sku_price_key);
            $sku_goods_id = $parse_key[0];
            $exist_pos = false;
            foreach ($goods_list as $k => $v) {
                if ($v['goods_id'] == $sku_goods_id && $v['goods_sku_price_id'] == $sku_price_val) {
                    $exist_pos = $k;
                    break;
                }
            }
            if ($exist_pos === false) {
                array_push($goods_list, [
                    'goods_id' => (int) $sku_goods_id,
                    'goods_sku_price_id' => (int) $sku_price_val,
                    'goods_num' => 1,
                ]);
            } else {
                $goods_list[$exist_pos]['goods_num'] += 1;
            }
        }
        foreach ($goods_list as $k => $v) {
            foreach ($goods as $goods_item) {
                if ($goods_item->id == $v['goods_id']) {
                    $goods_list[$k]['goods_title'] = $goods_item->title;
                    $goods_list[$k]['goods_image'] = $goods_item->image;
                    $goods_original_amount = bcadd($goods_original_amount, $goods_item->original_price, 2);
                    break;
                }
            }
        }
        $sku_goods_sku_price = DB::table('goods_sku_price')->whereIn('id', array_column($goods_list, 'goods_sku_price_id'))->get();
        foreach ($goods_list as $k => $v) {
            foreach ($sku_goods_sku_price as $sku_goods_sku_price_item) {
                if ($sku_goods_sku_price_item->id == $v['goods_sku_price_id']) {
                    $goods_amount = bcadd($goods_amount, $sku_goods_sku_price_item->price, 2);
                    $goods_list[$k]['goods_price'] = floatval($sku_goods_sku_price_item->price);
                    $goods_list[$k]['goods_sku_price_text'] = $sku_goods_sku_price_item->goods_sku_text;
                    break;
                }
            }
        }

        $card_list = [];
        foreach ($card_info as $card_val) {
            $total_num += $card_val['number'];
            array_push($card_list, ['card_id' => (int) $card_val['id'], 'card_num' => (int) $card_val['number']]);
        }
        $card = DB::table('pickgoods_card')->whereIn('id', array_column($card_list, 'card_id'))->get();
        foreach ($card_list as $k => $v) {
            foreach ($card as $card_item) {
                if ($card_item->id == $v['card_id']) {
                    $card_list[$k]['card_name'] = $card_item->name;
                    $card_list[$k]['card_amount'] = floatval($card_item->amount);
                    $card_list[$k]['card_goods_ids'] = $card_item->goods_ids;
                    $card_list[$k]['card_valid_day'] = (int) $card_item->valid_day;
                    $card_list[$k]['card_validtime_start'] = $card_item->validtime_start;
                    $card_list[$k]['card_validtime_end'] = $card_item->validtime_end;
                    break;
                }
            }
        }

        if (!empty($goods_list)) {
            $need_address = 1;
        }

        $result = [
            'need_address' => $need_address,
            'combo_goods' => $combo_goods,
            'combo_goods_sku' => $combo_goods_sku,
            'total_num' => $total_num,
            'goods_amount' => floatval($goods_amount),
            'goods_original_amount' => floatval($goods_original_amount),
            'total_fee' => floatval($combo_goods->price),
            'goods_list' => $goods_list,
            'card_list' => $card_list,
        ];

        // 随机立减
        $offprice_config = config::get('order');
        if (!empty($offprice_config['offprice_enable'])) {
            $min = intval(($offprice_config['offprice_min'] ?? 0) * 100);
            $max = intval(($offprice_config['offprice_max'] ?? 0) * 100);
            $result['off'] = [$min, $max];
        }

        return $result;
    }
}

